/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chuiniurobot.message;

import chuiniurobot.GameStatusEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chuiniu.model.ResponseCode;
import com.chuiniu.server.command.BaseCommand;
import java.util.HashMap;
import java.util.Map;
import org.apache.mina.core.session.IoSession;

/**
 *接收到对战请求
 * @author zhengji
 */
public class PKMessage extends BaseMessage{

    int vsPlayerId;
    public PKMessage(IoSession ioSession, String requestJSONStr) {
        super(ioSession, requestJSONStr);
        
         JSONObject jsonObj = (JSONObject) JSON.parse(this.requestJSONStr);
    
         this.vsPlayerId=jsonObj.getIntValue("id");//对家标识
    }

    @Override
    public void execute() {
        
        
        Map<String,String> map=new HashMap<String,String>();
        
        map.put("cmd", BaseMessage.RequestPKCommand);
        
        map.put("id", String.valueOf(this.getPlayerId()));
        map.put("vsid", String.valueOf(this.vsPlayerId));
        if(this.getGameStatus()== GameStatusEnum.GameStatusPlaying){
            //正在对战
            map.put("c",String.valueOf( ResponseCode.PlayerAlreadyPK));
        }else{
            //接受请求
            map.put("c", String.valueOf(ResponseCode.Success));
            
            this.setGameStatus(GameStatusEnum.GameStatusPlaying);
            
            //sendReadyPlayMessage();//发送准备好开始消息
        }
        
        this.sendMessage(map);
    }

   
    
    
}
